from PyQt4 import QtGui,QtCore,uic

import Machine

class MachineSettingsWidget(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)

        uic.loadUi("ui/ui_files/MachineSettingsWidget.ui", self)

        machine = Machine.getCurrentMachine()

        self.nameEdit.setText(machine.getName())
        self.typeCombo.setCurrentIndex(machine.getType())
        self.maxRpmSpin.setValue(machine.getMaxRpm())
        self.unitTypeCombo.setCurrentIndex(machine.getUnitType())

        self.typeCombo.currentIndexChanged.connect(self.indexChanged)

    def __del__(self):
        machine = Machine.getCurrentMachine()

        machine.setName(str(self.nameEdit.text()))
        
        if self.typeCombo.currentIndex() == 0:
            machine.setType(Machine.MachineType.Lathe)
        elif self.typeCombo.currentIndex() == 1:
            machine.setType(Machine.MachineType.ThreeAxisMill)
        elif self.typeCombo.currentIndex() == 2:
            machine.setType(Machine.MachineType.FourAxisMill)

        machine.setMaxRpm(self.maxRpmSpin.value())

        if self.unitTypeCombo.currentIndex() == 0:
            machine.setUnitType(Machine.UnitType.Metric)
        elif self.unitTypeCombo.currentIndex() == 1:
            machine.setUnitType(Machine.UnitType.Imperial)

    def indexChanged(self, index):
        machine = Machine.getCurrentMachine()

        if index == 0: machine.setType(Machine.MachineType.Lathe)
        elif index == 1: machine.setType(Machine.MachineType.ThreeAxisMill)
        elif index == 2: machine.setType(Machine.MachineType.FourAxisMill)

        self.window().updateAxisItems()
